<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form @submit.native.prevent>
        <el-select style="width: 200px" v-model="listQuery.mode" placeholder="显示">
          <el-option
            v-for="(item, index) in [
              { id: -1, value: '全部' },
              { id: 1, value: '当前代理商' },
              { id: 2, value: '直属下级代理商' },
            ]"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>

        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          native-type="submit"
          @click="fetchData(1)"
          >查询</el-button
        >
      </el-form>
    </div>
    <!-- 表格内容 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
      @sort-change="sortChange"
    >
      <el-table-column label="上级代理商">
        <template slot-scope="scope">{{ scope.row.up_agent && scope.row.up_agent.name }}</template>
      </el-table-column>
      <el-table-column label="代理商名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="联系人">
        <template slot-scope="scope">{{ scope.row.contact_name }}</template>
      </el-table-column>
      <el-table-column label="联系电话">
        <template slot-scope="scope">{{ scope.row.contact_mobile }}</template>
      </el-table-column>
      <el-table-column label="提现用户码">
        <template slot-scope="scope">{{ scope.row.transfer_open_id }}</template>
      </el-table-column>
      <el-table-column label="待结算分润" prop="shareprofit" sortable="custom">
        <template slot-scope="scope">
          <el-link type="primary" @click="$router.push(`/share/list?agent_id=${scope.row.id}&status=${0}`)">{{
            scope.row.shareprofit
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="累计分润" prop="total_shareprofit" sortable="custom">
        <template slot-scope="scope">{{ scope.row.total_shareprofit }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="settleHandler(scope.row)"
            v-if="scope.row.shareprofit > 0 && scope.row.up_agent_id === $store.getters.agent.id"
            >结算</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.size"
      @pagination="fetchData"
    />
  </div>
</template>

<script>
import waves from '@/directive/waves'; // waves directive
import Pagination from '@/components/Pagination'; // secondary package based on el-pagination
import { getList } from '@/api/agent';
import { settle } from '@/api/share';

export default {
  directives: {
    waves,
  },
  components: {
    Pagination,
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1, // 当前页码
        size: 50, // 每页大小

        sortProp: 'shareprofit',
        sortOrder: 'desc',

        mode: this.$route.query.mode !== undefined ? Number(this.$route.query.mode) : -1,
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    sortChange(e) {
      this.listQuery.sortProp = e.prop;
      if (e.order === 'ascending') {
        this.listQuery.sortOrder = 'asc';
      } else if (e.order === 'descending') {
        this.listQuery.sortOrder = 'desc';
      } else {
        this.listQuery.sortOrder = null;
      }
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const res = await getList(this.listQuery);
      this.total = res.data.total;
      this.list = res.data.list;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    settleHandler(row) {
      this.$confirm(
        '确认结算吗?' + (row.transfer_open_id ? `将执行自动提现！用户将收到￥${row.shareprofit}` : ''),
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info',
        }
      ).then(async () => {
        const res = await settle({ agent_id: row.id });
        this.$message({
          type: 'success',
          message: res.msg,
        });
        this.fetchData();
      });
    },
  },
};
</script>
<style scoped lang="scss">
.btn {
  // margin: 5px;
}

.filter-container {
  .filter-item {
    margin-bottom: 10px;
  }
}
</style>
